package com.atguigu.crowd.handler;

import com.atguigu.crowd.entity.vo.DetailProjectVO;
import com.atguigu.crowd.entity.vo.PortalProjectVO;
import com.atguigu.crowd.entity.vo.PortalTypeVO;
import com.atguigu.crowd.entity.vo.ProjectVO;
import com.atguigu.crowd.service.api.ProjectService;
import com.atguigu.crowd.util.ResultEntity;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ProjectProviderHandler {
    @Autowired
    private ProjectService projectService;




    //更多页面 其他
    @RequestMapping("/get/more/other")
    public ResultEntity<List<PortalProjectVO>> getMoreProjectOtherRemote(@RequestParam("typeId") Integer typeId) {
        try {

            List<PortalProjectVO> moreOther = projectService.getMoreOther(typeId);
            return  ResultEntity.successWithData(moreOther);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }


    }


    //更多页面全部
    @RequestMapping("/get/more/all")
    public ResultEntity<List<PortalProjectVO>> getMoreProjectAllRemote(){
        try {

            List<PortalProjectVO> moreAll = projectService.getMoreAll();
            return  ResultEntity.successWithData(moreAll);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    //申请延期功能 修改project表的delayDay delayNumber developmentDay字段
    @RequestMapping("/update/member/applyDelay/remote")
    public ResultEntity<String>updateMemberApplyRemote(
            @RequestParam("projectId")Integer projectId,
            @RequestParam("delayDay")Integer delayDay,
            @RequestParam("delayNumber")Integer delayNumber
    ){
        try {
            // 调用“本地”Service执行保存
            int affectRows = projectService.updateMemberProjectApply(projectId, delayDay, delayNumber);
//            System.out.println("affectRows: "+affectRows);
                    return ResultEntity.successWithData("申请通过");

        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }



    //获取用户发起的项目
    @RequestMapping("/get/member/my/crowd/project/remote")
    public ResultEntity<List<DetailProjectVO>> getMemberCrowdProjectRemote( @RequestParam("memberId")Integer memberId) {
        try {
            List<DetailProjectVO> ProjectVOMemberList = projectService.getMemberCrowdProjectDeploy(memberId);
            return ResultEntity.successWithData(ProjectVOMemberList);
        }catch (Exception e){
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
    /**
     * 项目发起 收集的所有项目相关信息 进行保存进mysql
     * @param projectVO
     * @param memberId
     * @return
     */
    @ResponseBody
    @RequestMapping("/save/project/vo/remote")
    public ResultEntity<String> saveProjectVORemote(
            @RequestBody ProjectVO projectVO,
            @RequestParam("memberId") Integer memberId) {

        try {
            // 调用“本地”Service执行保存
            projectService.saveProject(projectVO, memberId);

            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

    }

    /**
     * 查询获得PortalTypeVO信息
     * @return
     */
    @RequestMapping("/get/portal/type/project/data/remote")
    public ResultEntity<List<PortalTypeVO>> getPortalTypeProjectDataRemote() {
        try {
            List<PortalTypeVO> portalTypeVOList = projectService.getPortalTypeVO();
            return ResultEntity.successWithData(portalTypeVOList);
        }catch (Exception e){
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    /**
     * 根据项目ID查询项目详情信息
     */
    @RequestMapping("/get/project/detail/remote/{projectId}")
    public ResultEntity<DetailProjectVO> getDetailProjectVORemote(@PathVariable("projectId") Integer projectId) {
        try {
            DetailProjectVO detailProjectVO = projectService.getDetailProjectVO(projectId);
            return ResultEntity.successWithData(detailProjectVO);
        }catch (Exception e){
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }


    }
